/**
 * Eclipse Outliner
 * Copyright (C) 2006 Jeff Mesnil
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License. 
 */
package net.jmesnil.opml.core.internal;

import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

import net.jmesnil.opml.core.IOutline;
import net.jmesnil.opml.core.IOutlineNode;

import org.eclipse.core.runtime.Assert;
import org.eclipse.core.runtime.PlatformObject;

public class Outline extends PlatformObject implements IOutline {

    protected IOutlineNode parent;

    private String text;

    private Date creation;

    private List children;

    protected Outline(IOutlineNode parent, String text, Date creation) {
        Assert.isNotNull(parent);
        Assert.isNotNull(text);
        Assert.isNotNull(creation);

        this.parent = parent;
        this.text = text;
        this.creation = creation;
        children = new ArrayList();
    }

    public IOutline[] getChildren() {
        return (IOutline[]) children.toArray(new IOutline[children.size()]);
    }

    public IOutline removeChild(IOutline outlineToRemove) {
        Outline outline = (Outline) outlineToRemove;
        boolean found = children.remove(outline);
        if (!found) {
            return null;
        }
        outline.setParent(null);
        return outline;
    }

    public IOutline getPreviousSibling() {
        if (this.parent == null) {
            return null;
        }
        IOutline[] siblings = parent.getChildren();
        int index = indexOf(this, siblings);
        if (index == -1) {
            return null;
        }
        if (index == 0) {
            return null;
        }
        return (IOutline) siblings[index - 1];
    }

    public IOutline getNextSibling() {
        if (this.parent == null) {
            return null;
        }
        IOutline[] siblings = parent.getChildren();
        int index = indexOf(this, siblings);
        if (index == -1) {
            return null;
        }
        if (index == indexOfLast(siblings)) {
            return null;
        }
        return (IOutline) siblings[index + 1];
    }

    private int indexOfLast(IOutline[] siblings) {
        return siblings.length - 1;
    }

    private int indexOf(Outline child, IOutline[] children) {
        for (int i = 0; i < children.length; i++) {
            IOutline outline = children[i];
            if (child == outline) {
                return i;
            }
        }
        return -1;
    }

    public Date getCreation() {
        return creation;
    }

    public IOutlineNode getParent() {
        return parent;
    }

    public String getText() {
        return text;
    }

    public void setText(String text) {
        Assert.isNotNull(text);
        this.text = text;
    }

    public IOutline insertAfter(String text, IOutline refOutline) {
        int index = children.indexOf(refOutline);
        Outline newOutline = new Outline(this, text, new Date());
        if (index == -1) {
            children.add(newOutline);
        } else {
            children.add(index + 1, newOutline);
        }
        return newOutline;
    }

    public IOutline insertAfter(IOutline outline, IOutlineNode refOutline) {
        if (outline == null) {
            return null;
        }
        int index = children.indexOf(refOutline);
        if (index == -1) {
            return null;
        } else {
            Outline outlineImpl = (Outline) outline;
            IOutlineNode oldParent = outlineImpl.parent;
            outlineImpl.parent = this;
            children.add(index + 1, outline);
            if (oldParent != null) {
                oldParent.removeChild(outline);
            }
        }
        return outline;
    }

    public IOutline appendNewOutline(String text) {
        Outline newOutline = new Outline(this, text, new Date());
        children.add(newOutline);
        return newOutline;
    }

    public IOutline appendChild(IOutline outline) {
        Outline outlineImpl = (Outline) outline;
        outlineImpl.parent = this;
        children.add(outlineImpl);
        return outlineImpl;
    }

    public String toXML() {
        StringBuffer xml = new StringBuffer("<outline text=\"" + text + "\">\n");
        for (Iterator iter = children.iterator(); iter.hasNext();) {
            IOutline outline = (IOutline) iter.next();
            xml.append(outline.toXML());
        }
        xml.append("</outline>\n");
        return xml.toString();
    }

    public String toString() {
        return "Outline {" + text + "}";
    }

    void setParent(IOutlineNode newParent) {
        this.parent = newParent;
    }
}
